<?php 
/**
 * 73yc 教师系统订单输出
 */
define('IN_ECS', true);
require('includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/cls_json.php');
$json = new JSON();
/* 任务操作 */
$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'default';
$url = "http://tch.eggms.com/teacher_api.php";//地址
$KeyID = "21232f297a57a5a743894a0e4a801fc3";//密钥ID
$KeyPass = "0192023a7bbd73250516f069df18b500";//密钥
/* 推送订单 */
if($action == 'pushtrades'){
  $time = '1425657600';
  $order= array();
  $sql= " SELECT add_time, order_id,order_sn,user_id,consignee,pay_name, (goods_amount+shipping_fee) AS amount FROM ".$ecs->table('order_info') .
        " WHERE is_teacher != 1 AND add_time > $time AND goods_amount !=0 AND shipping_status > 0  AND pay_status = 2 ".
        " LIMIT 0 , 10";
  $order = $db->getAll($sql);
  foreach ($order as $key => $value) {
    $order_list[$value['order_id']]=$value;
  }
  $order_list['KeyID']=$KeyID;
  $order_list['KeyPass']=$KeyPass;

  $a=vpost($url,vdata($order_list));//提交数据
  //标记订单为已推送
  foreach ($order_list as $order_id => $order_value) {
    if($order_id!='KeyID'&&$order_id!='KeyPass'){
        $sql="UPDATE ".$GLOBALS['ecs']->table('order_info').
             " set is_teacher=1".
             " WHERE order_id=$order_id";
        $db->query($sql);
    }
  }

}



/**
 * 邮件发送
 *
 * @param: $url[string]        请求地址
 * @param: $data[json]       请求数据
 *
 * @return $tmpInfo 数据
 */
function vpost($url,$data=""){ // 模拟提交数据函数
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
    // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
       echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    require_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON();
    return $json->decode($tmpInfo,true); // 返回数据
}

/* 处理请求数据 */
function vdata($data = ''){
    if(is_array($data)){
        require_once(ROOT_PATH . 'includes/cls_json.php');
        $json = new JSON();
        $data = 'v_data='.urlencode(json_encode($data,JSON_UNESCAPED_UNICODE));
    }else{
        $data = 'v_data='.urlencode($data);
    }
    return $data;
}



 ?>